package _mst;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Objects;

/**
 * 面试题 03.02. 栈的最小值
 */
public class T0302 {
    private final Deque<Integer> stack;
    private final Deque<Integer> minStack;

    public T0302() {
        stack = new LinkedList<>();
        minStack = new LinkedList<>();
    }

    public void push(int x) {
        stack.push(x);
        if (minStack.isEmpty() || x <= minStack.peek()) minStack.push(x);
    }

    public void pop() {
        Integer pop = stack.pop();
        if (Objects.equals(minStack.peek(), pop)) minStack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}
